﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LL.Business;
using NanoERP.Business;
using NanoERP.Model.Poco;
using NanoERP.Model.SystemMgmt;
using Omu.ValueInjecter;

namespace NanoERP.Business.Permission
{
    public class DataDictionaryService : ServiceBase
    {
        #region CRUD

        public SYS_DataDictionary GetDic(Guid id)
        {
            return DbSession.Get<SYS_DataDictionary>(id);
        }

        public void AddDic(SYS_DataDictionary user)
        {
            DbSession.Insert(user);
        }

        public void UpdateDic(SYS_DataDictionary user)
        {
            DbSession.Update(user);
        }

        public void AddDicType(SYS_DataDictionaryType roleType)
        {
            DbSession.Insert(roleType);
        }

        public void UpdateDicType(SYS_DataDictionaryType roleType)
        {
            DbSession.Update(roleType);
        }

        public SYS_DataDictionaryType GetDicType(Guid id)
        {
            return DbSession.Get<SYS_DataDictionaryType>(id);
        }

        public void MoveType(Guid id, Guid? desId)
        {
            var d = DbSession.Get<SYS_DataDictionaryType>(id);
            d.ParentId = desId;
            DbSession.Update(d);
        }

        public void Move(Guid id, Guid desId)
        {
            var d = DbSession.Get<SYS_DataDictionary>(id);
            d.TypeId = desId;
            DbSession.Update(d); ;
        }

        public void DeleteDic(Guid id)
        {
            DbSession.Delete<SYS_DataDictionary>(id);
        }

        public void DeleteDicType(Guid id)
        {
            DbSession.Delete<SYS_DataDictionaryType>(id);
        }

        #endregion

        public IList<SYS_DataDictionary> GetDictionaryListByTypeCode(string typeCode)
        {
            var type = DbSession
                 .Query<SYS_DataDictionaryType>("TypeCode=@0", typeCode)
                 .FirstOrDefault();
            if (type == null)
                return new List<SYS_DataDictionary>(1);
            return DbSession.Query<SYS_DataDictionary>("TypeId=@0", type.Id);
        }

        public DataDictionaryTree GetDicTree()
        {
            var root = new DataDictionaryTree();
            var dics = DbSession.GetAll<SYS_DataDictionary>();
            var types = DbSession.GetAll<SYS_DataDictionaryType>();
            //获取顶层字典
            foreach (var dicType in
                (from d in types where d.ParentId == null select d))
            {
                var newItem = new DataDictionaryTree();
                newItem.InjectFrom(dicType);
                FillSubDataDictionaryTree(newItem, types, dics);
                root.Children.Add(newItem);
            }
            return root;
        }

        private void FillSubDataDictionaryTree(DataDictionaryTree item, IEnumerable<SYS_DataDictionaryType> types, IEnumerable<SYS_DataDictionary> dics)
        {
            var children = from d in types where d.ParentId == item.Id select d;
            foreach (var child in children)
            {
                var newItem = new DataDictionaryTree();
                newItem.InjectFrom(child);
                FillSubDataDictionaryTree(newItem, types, dics);
                item.Children.Add(newItem);
            }
            foreach (var dic in
                    (from dd in dics where dd.TypeId == item.Id select dd))
            {
                item.Items.Add(dic);
            }
        }

    }
}
